<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">工作单位</h4>
</div>
<div class="modal-body">

    <form id="form_data" class="form-horizontal row">
        <div id="div_error" class="col-sm-12"></div>
        <input type="hidden" id="id" name="id" value="${data.id}">
        <input type="hidden" name="expertId" value="${data.expertId}">
        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">单位名称：</label>
            <div class="col-sm-6">
                <select class="select2-ajax  form-control" id="name" name="name">
                    <option value="${data.name}">${data.name}</option>
                </select>
            </div>
        </div>
        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">单位代码：</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="code" name="code" value="${data.code}">
            </div>
        </div>
        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">单位地址：</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="address" value="${data.address}">
            </div>
        </div>
        <div class="form-group col-sm-6">
            <label class="col-sm-4 control-label">单位性质：</label>
            <div class="col-sm-6">
                <select id="property" class="form-control" name="property">
                    <option value="">&nbsp;</option>
                </select>
            </div>
        </div>

        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">联系方式：</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" name="phone" value="${data.phone}">
            </div>
        </div>

        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">所属机构：</label>
            <div class="col-sm-6">
                <input type="hidden" id="affiliate" name="affiliate" value="${data.affiliate}">
                <select class="select2-ajax  form-control" id="affiliateTemp" name="affiliateTemp">
                    <option value="">&nbsp;</option>
                </select>
            </div>
        </div>

        <div class="form-group col-sm-6">
            <label class="control-label col-sm-4">所属机构代码：</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="affiliateCode" name="affiliateCode"
                       value="${data.affiliateCode}">
            </div>
        </div>


        <div class="form-group col-sm-6">
            <div class="col-sm-6 col-sm-offset-4">
                <button type="submit"
                        class="btn btn-default btn-success"><span
                        class="glyphicon glyphicon-ok"></span>&nbsp;确&nbsp;定&nbsp;
                </button>
            </div>
        </div>

    </form>

    <br>
</div>

<script>


    $.sysPlugin.initSelect2($("#property"), {
        data: $.sysPlugin.enumeration.companyProperty,
        value: '${data.property}'
    });

    var select2cfg = {
        ajax: {
            url: "common/companyPool/getCompanyByNameLike",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term,
                    page: params.page
                };
            },
            processResults: function (data, params) {
                params.page = params.page || 1;
                return {
                    results: data.items,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) {
            console.log(markup);
            return markup;
        },
        minimumInputLength: 1,
        templateResult: function (repo) {
            if (repo.loading) return repo.text;
            var markup;
            if (!repo.data) {
                markup = '提示：将输入框内容作为单位名称（' + repo.text + '）';
            } else {
                markup = repo.text;
            }
            return markup;
        },
        templateSelection: function (repo) {

            if (repo.data) {
                $("#code").val(repo.data);
            }
            return repo.text;
        },
        language: "zh-CN",
        tags: true,
        width: '100%'
    };

    $("#name").select2(select2cfg);

    // $.extend(select2cfg, {
    //     templateSelection: function (repo) {
    //         if(repo.data){
    //             $("#affiliateCode").val(repo.data);
    //         }
    //
    //         return repo.text;
    //     }
    // });
    //
    // $("#affiliate").select2(select2cfg);


    $("#affiliateTemp").change(function () {
        var temp = $("#affiliateTemp option:selected");
        $("#affiliateCode").val(temp.val());
        if (temp.val() == "") {
            $("#affiliate").val("");
        } else {
            $("#affiliate").val(temp.text());
        }
    });

    $.sysPlugin.initSelect2($("#affiliateTemp"), {
        url: 'dictionary/publicOpen/affiliateCompanyOnlyLive',
        value: '${data.affiliateCode}',
    });


    <%--bootstrapValidator--%>
    var $form = $("#form_data");
    $form.bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }
                }
            }

        }
    }).on('success.form.bv', function (e) {
        e.preventDefault();

        $.sysPlugin.superAjax({
            data: $(e.target).serialize(),
            url: '${url}',
            operateType: '${operateType}',
            showSuccessAlert: true,
            showWarnAlert: false,
            successCall: function (data) {
                $.sysPlugin.successCallBack(data, {
                    modal: $("#modal_ajax"),
                });
                refreshTab($("#tab_expertCompany"));
            },
            errorCall: function (data) {
                $.sysPlugin.errorCallBack(data, {
                    errorAddress: $("#div_error"),
                    form: $form
                })
            }
        });
    });
</script>
